mui.init();

document.addEventListener("updateDataList", function () {
	myOrderList.getMyOrderData('normal');
});

var myOrderList;
var page = 0;
var listRows = 3;
mui.plusReady(function() {
	hideScrollBar();
	myOrderList = new Vue({
		el: '#my_order',
		data: {
			tabIndex: 1,
			order_type:'course',

			course_order:[],
			shop_order:[],
			ebook_order:[],

			page:1,
			npage:1,
			cpage:1,
			epage:1,
			spage:1,

			miniRefreshArr:[],
		},
		watch:{
			tabIndex:function (val,oldVal) {
				var self = this;
				if(val == 1){
					self.order_type = 'course';
				}else if(val == 2){
					self.order_type = 'shop';
				}else if(val == 3){
					self.order_type = 'ebook';
				}
				if(self.miniRefreshArr[val-1] == undefined){
					self.$nextTick(function () {
						self.miniRefreshArr[val-1] = new MiniRefresh({
							container: '#minirefresh' + val,
							down: {
								isAuto: true,
								callback: function() {
									self.getMyOrderData('normal');
								}
							},
							up: {
								isAuto: false,
								offset:10,
								loadFull:{
									isEnable:false,
								},
								callback: function() {
									self.getMyOrderData('up');
								}
							}
						});
					})

				}else{
					self.getMyOrderData('tab');
				}
			}
		},
		mounted() {
			var self = this;
			self.$nextTick(function() {
				var slider = mui("#slider");
				slider.slider({
					interval: 0
				});
				self.miniRefreshArr[0] = new MiniRefresh({
					container: '#minirefresh1',
					down: {
						isAuto: true,
						callback: function() {
							self.getMyOrderData('normal');
						}
					},
					up: {
						isAuto: false,
						offset:10,
						loadFull:{
							isEnable:false,
						},
						callback: function() {
							self.getMyOrderData('up');
						}
					}
				});
			})
		},
		methods: {
			select_tap: function(e) {
				this.tabIndex = parseInt(e.target.getAttribute('tab-index'));
			},
			getMyOrderData: function(type) {
				var self = this;
				if(type == 'normal'){
					self.miniRefreshArr[self.tabIndex - 1].resetUpLoading();
					self.page = 1;
					self.npage = 1;
					self.cpage = 1;
					self.epage = 1;
					self.course_order.length = 0;
					self.shop_order.length = 0;
					self.ebook_order.length = 0;
				}else if(type == 'up'){
					switch (self.tabIndex) {
						case 1:
							self.npage++;
							self.page = self.npage;
							break;
						case 2:
							self.cpage++;
							self.page = self.cpage;
							break;
						case 3:
							self.epage++;
							self.page = self.epage;
							break;
					}
				}else if(type == 'tab'){
					self.miniRefreshArr[self.tabIndex - 1].resetUpLoading();
					switch (self.tabIndex) {
						case 1:
							if (self.course_order.length == 0) {
								self.page = self.npage = 1;
							}else{
								return false;
							}
							break;
						case 2:
							if (self.shop_order.length == 0) {
								self.page = self.cpage = 1;
							}else{
								return false;
							}
							break;
						case 3:
							if (self.ebook_order.length == 0) {
								self.page = self.epage = 1;
							}else{
								return false;
							}
							break;
					}
				}
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/user/getMyOrderData',
					data: {
						'order_type': self.order_type,
						'page':self.page,
					},
					dataType: 'json',
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							if(self.tabIndex == 1){
								$.each(msg.data, function (index, value) {
									self.course_order.push(value);
								});
							}else if(self.tabIndex == 2){
								$.each(msg.data, function (index, value) {
									self.shop_order.push(value);
								});
							}else if(self.tabIndex == 3){
								$.each(msg.data, function (index, value) {
									self.ebook_order.push(value);
								});
							}
							if(type == 'up'){
								self.miniRefreshArr[self.tabIndex - 1].endUpLoading(false, '成功更新'+msg.data.length+'条数据');
							}
						}else{
							if(type == 'up'){
								self.miniRefreshArr[self.tabIndex - 1].endUpLoading(true);
							}
						}
					},
					error: function(xhr, type, errorThrown) {
						mui.toast('获取我的订单失败,请重新尝试', {duration: '2000',type: 'div'});
					},
					complete: function() {
						if(type == 'normal'){
							setTimeout(function () {
								self.miniRefreshArr[self.tabIndex - 1].endDownLoading(true);
							},500);
						}
					}
				})
			},
			open_myOrderDetail: function(id) {
				mui.openWindow({
					url: 'orderDetail.html',
					extras: {
						'oid': id,
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					},
				});
			}
		}
	})
})

